﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SDFL.Utilities
{
    public class DirecotryHelper
    {
        /// <summary>
        /// Does a single level (non-recursive) directory copy.
        /// </summary>
        /// <param name="sourceDir">The source directory</param>
        /// <param name="destinationDir">The destination directory</param>            
        public static void CopyDirectory(string sourceDir, string destinationDir)
        {
            DeleteDirectory(destinationDir);

            DirectoryInfo destination = Directory.CreateDirectory(destinationDir);
            //Copy Policheck down to the machine
            foreach (string file in Directory.GetFiles(sourceDir))
            {
                FileInfo fi = new FileInfo(file);
                File.Copy(fi.FullName, Path.Combine(destination.FullName, fi.Name), true);
            }
        }

        /// <summary>
        ///  Delete folder, remove "readonly" attribute firstly.
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        public static bool DeleteDirectory(string dir)
        {
            if (Directory.Exists(dir))
            {
                foreach (string file in Directory.GetFiles(dir))
                {
                    FileInfo fi = new FileInfo(file);
                    if ((fi.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                    {
                        fi.Attributes = fi.Attributes ^ FileAttributes.ReadOnly;
                    }
                }
                Directory.Delete(dir, true);
                return true;
            }
            else
                return false;
        }
    }
}
